package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.Brand;
import com.ridaizu.model.Model;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "brandDao")
@ApplicationScoped
public class BrandDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from m_brand \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public Map<String, Brand> selectBrandNameMap() throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id, name FROM m_brand \n");

		ResultSet rs = DBAccess.query(sql.toString());

		Map<String, Brand> brandMap = new HashMap<String, Brand> ();
		while (rs.next()) {
			Brand brand = new Brand();
			brand.setId(rs.getInt("id"));
			brand.setName(rs.getString("name"));

			brandMap.put(brand.getName(), brand);
		}

		rs.close();

		return brandMap;
	}

	public Map<Integer, Brand> selectBrandIdMap() throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id, name FROM m_brand \n");

		ResultSet rs = DBAccess.query(sql.toString());

		Map<Integer, Brand> brandMap = new HashMap<Integer, Brand> ();
		while (rs.next()) {
			Brand brand = new Brand();
			brand.setId(rs.getInt("id"));
			brand.setName(rs.getString("name"));

			brandMap.put(brand.getId(), brand);
		}

		rs.close();

		return brandMap;
	}

	public List<Brand> selectBrandList() throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id, name FROM m_brand \n");

		ResultSet rs = DBAccess.query(sql.toString());

		List<Brand> modelList = new ArrayList<Brand> ();
		while (rs.next()) {
			Brand brand = new Brand();
			brand.setId(rs.getInt("id"));
			brand.setName(rs.getString("name"));

			modelList.add(brand);
		}

		rs.close();

		return modelList;
	}

	public List<Brand> selectBrandList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM m_brand \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<Brand> modelList = new ArrayList<Brand> ();
		while (rs.next()) {
			modelList.add((Brand)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		Brand brand = new Brand();
		brand.setId(rs.getInt("id"));
		brand.setName(rs.getString("name"));
		brand.setSummary(rs.getString("summary"));
		brand.setDetail(rs.getString("detail"));
		brand.setHomepage(rs.getString("homepage"));
		brand.setPhotoUrl(rs.getString("photo_url"));
		brand.setThumbnailsUrl(rs.getString("thumbnails_url"));
		brand.setDelFlg(rs.getBoolean("del_flg"));
		brand.setRgtOpt(rs.getString("rgt_opt"));
		brand.setRgtDate(rs.getTimestamp("rgt_date"));
		brand.setUpdOpt(rs.getString("upd_opt"));
		brand.setUpdDate(rs.getTimestamp("upd_date"));

		return brand;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO m_brand \n");
		sql.append("( \n");
		sql.append("name, summary, detail, homepage, \n");
		sql.append("photo_url, thumbnails_url, del_flg, rgt_opt, \n");
		sql.append("rgt_date, upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); \n");

		Brand brand = (Brand) model;

		Object[] params = new Object[] {
				brand.getName(), brand.getSummary(), brand.getDetail(), brand.getHomepage(),
				brand.getPhotoUrl(), brand.getThumbnailsUrl(), brand.isDelFlg(), brand.getRgtOpt(),
				brand.getRgtDate(), brand.getUpdOpt(), brand.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE m_brand \n");
		sql.append("SET \n");
		sql.append("name = ?, \n");
		sql.append("summary = ?, \n");
		sql.append("detail = ?, \n");
		sql.append("homepage = ?, \n");
		sql.append("photo_url = ?, \n");
		sql.append("thumbnails_url = ?, \n");
		sql.append("del_flg = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		Brand brand = (Brand) model;

		Object[] params = new Object[] {
				brand.getName(), brand.getSummary(), brand.getDetail(), brand.getHomepage(),
				brand.getPhotoUrl(), brand.getThumbnailsUrl(), brand.isDelFlg(), brand.getRgtOpt(),
				brand.getRgtDate(), brand.getUpdOpt(), brand.getUpdDate(), brand.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM m_brand WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
